import {EventEmitter} from "events";

//EventEmitter 类来绑定和监听事件。
export class TextModel extends EventEmitter {
    //待渲染数据
    private spacers_: string = ''

    constructor() {
        super();
    }

    //当数据模型改变时，发布
    textChangeEmit_():void {
        this.emit('text-change');
    }

    getSpacers():string {
        return this.spacers_
    }

    insert(spacerIndex: number, spacers: string): void {
        const oldSpacers = this.spacers_
        this.spacers_ = oldSpacers.slice(0, spacerIndex) + spacers + oldSpacers.slice(spacerIndex)
        this.textChangeEmit_()
    }

    remove(startIndex: number, endIndex: number): void {
        const oldSpacers = this.spacers_
        if (startIndex > endIndex) {
            [startIndex, endIndex] = [endIndex, startIndex]
        }
        this.spacers_ = oldSpacers.slice(0, startIndex) + oldSpacers.slice(endIndex)
        this.textChangeEmit_()
    }
}